home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / audio / ascope / ascope.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.0 KB  |  129 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *  ascope.c -
  19.  *
  20.  *    cc -o ascope ascope.c -lgl_s -laudio
  21.  */
  22.  
  23. #include <audio.h>
  24. #include <gl.h>
  25. #include <gl/device.h>
  26.  
  27. #define  BUF_LEN  2205         
  28.  
  29. static void draw_buffer(short *, int);
  30.  
  31. main()
  32. {
  33.     int         done = 0;
  34.     int      bufferLength = BUF_LEN; 
  35.     short    *sampleBuffer;
  36.     ALport   port;
  37.     long     dev;
  38.     short    val;
  39.  
  40.     /*
  41.      * old-style GL initialization code
  42.      */
  43.     prefsize(500, 250);
  44.     winopen("scope");
  45.     ortho2(0.0, (float)bufferLength, -32767.0, 32767.0);
  46.     doublebuffer();
  47.     gconfig();
  48.     qdevice(ESCKEY);
  49.  
  50.     /*
  51.      * the 0-valued ALconfig provides us with the default
  52.      * settings for port (100,000 sample queue, stereo input,
  53.      * 16-bit data).
  54.      */
  55.     port = ALopenport("scope input", "r", (ALconfig)0);
  56.     if (port == (ALport) 0) {
  57.     printf("failed to open the audio port\n");
  58.     exit(-1);
  59.     }
  60.  
  61.     /*
  62.      * allocate enough buffer space for bufferLength frames of stereo
  63.      * data.
  64.      */
  65.     sampleBuffer = (short *)malloc(sizeof(short) * 2 * bufferLength);
  66.     if (sampleBuffer == (short *) 0) {
  67.     printf("yikes...malloc failed...abort.\n");
  68.     exit(-1);
  69.     }
  70.  
  71.     while (!done)
  72.     {
  73.        if (qtest()) {
  74.         dev = qread(&val);
  75.         if (dev == ESCKEY) {
  76.         done = 1;
  77.         }
  78.        }
  79.        ALreadsamps(port, sampleBuffer, 2 * bufferLength); 
  80.        draw_buffer(sampleBuffer, bufferLength);
  81.     }
  82.     ALcloseport(port);
  83.     exit(1);
  84. }
  85.  
  86. static void
  87. draw_buffer(short *sample, int length)
  88. {
  89.     int i;
  90.     float vert[2];
  91.  
  92.     /* background */
  93.     color(8);
  94.     clear();
  95.  
  96.     /* grid */
  97.     color(0);
  98.     for (i = 1; i < 10; i++) {
  99.         bgnline();
  100.         vert[0] = (float) length * (float) i / 10.0;
  101.         vert[1] = -32767.0;
  102.         v2f(vert);
  103.         vert[1] = 32767.0;
  104.         v2f(vert);
  105.         endline(); 
  106.     }
  107.     for (i = 1; i < 6; i++) {
  108.         bgnline();
  109.         vert[0] = 0.0;
  110.         vert[1] = -32767.0 + 65534 * (float) i / 6.0;
  111.         v2f(vert);
  112.         vert[0] = (float) length;
  113.         v2f(vert);
  114.         endline();
  115.     }
  116.  
  117.     /* draw the waveform for the left channel data */
  118.     color(GREEN);
  119.     bgnline();
  120.     for (i = 0; i < length; i++)
  121.     {
  122.         vert[0] = (float) i;
  123.         vert[1] = (float) sample[2*i];
  124.         v2f(vert);
  125.     } 
  126.     endline();
  127.     swapbuffers();
  128. }
  129.